package com.huifaapp.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Toast;

import com.huifaapp.mall.R;

import java.util.ArrayList;
import java.util.List;


public class ChioceAdapter extends RecyclerView.Adapter<ChioceAdapter.MyViewHolder> {
    private List<Boolean> isChecked;    //每个条目是否为选中
    private List<String> numbers;       //每个条目应显示的数字
    private List<Integer> checkedIndex;   //保存选中的下标
    private List<CheckBox> checkBox;      //保存选中的checkBox
    private int maxChecked = 9;       //最多选中几个
    private int checkedNumber = 0;    //选中的个数
    private int length = 50;  //条目的总长

    private Context context;

    public ChioceAdapter(Context context) {
        this.context = context;
        isChecked = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            isChecked.add(false);
        }
        numbers = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            numbers.add("");
        }
        checkedIndex = new ArrayList<>();
        checkBox = new ArrayList<>();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.chioce_item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ChioceAdapter.MyViewHolder holder, final int position) {


        //  不要动
        holder.cb_checked.setChecked(isChecked.get(position));
        holder.cb_checked.setText(numbers.get(position));
        holder.cb_checked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean check;
                if (checkedNumber < maxChecked) {
                    if (!isChecked.get(position)) {
                        checkedIndex.add(position);
                        checkBox.add(holder.cb_checked);
                        checkedNumber++;
                        setText();
                    } else {
                        clearText(position, holder.cb_checked);
                    }
                    check = !isChecked.get(position);
                } else {
                    if (checkedNumber == maxChecked && isChecked.get(position)) {
                        clearText(position, holder.cb_checked);
                    } else {
                        Toast.makeText(context, "最多只能选" + maxChecked + "个", Toast.LENGTH_SHORT).show();
                    }
                    check = false;
                }
                isChecked.set(position, check);
                holder.cb_checked.setChecked(isChecked.get(position));
            }
        });
    }

    public void clearText(int position, CheckBox cb_checked) {
        for (int i = 0; i < checkedIndex.size(); i++) {
            if (checkedIndex.get(i) == position) {
                numbers.set(position, "");
                checkedIndex.remove(i);
                checkBox.remove(i);
                break;
            }
        }
        checkedNumber--;
        setText();
        numbers.set(position, "");
        cb_checked.setText("");
    }

    public void setText() {
        for (int i = 0; i < checkedIndex.size(); i++) {
            numbers.set(checkedIndex.get(i), (i + 1) + "");
            checkBox.get(i).setText(numbers.get(checkedIndex.get(i)));
        }
    }

    @Override
    public int getItemCount() {
        return 50;
//        return list==null?0:list.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public CheckBox cb_checked;

        public MyViewHolder(View itemView) {
            super(itemView);
            cb_checked = (CheckBox) itemView.findViewById(R.id.cb_checked);
        }
    }
}
